
#include <cstdio>
#include "PropertyBoard.h"

PropertyBoard::PropertyBoard(BWidget* parent) : BScrollWidget(parent)
{
	this->setAreaPolicy(Policy_Dynamic, Policy_Preferred);
	_resetFuncMap["BDialog"] = &PropertyBoard::resetDialog;
	_resetFuncMap["BLabel"] = &PropertyBoard::resetLabel;
	_resetFuncMap["BButton"] = &PropertyBoard::resetButton;
	_resetFuncMap["BSpinBox"] = &PropertyBoard::resetSpinBox;
	_resetFuncMap["BLayout"] = &PropertyBoard::resetLayout;
	_resetFuncMap["BHBoxLayout"] = &PropertyBoard::resetLayout;
	_resetFuncMap["BVBoxLayout"] = &PropertyBoard::resetLayout;
	_resetFuncMap["BStackLayout"] = &PropertyBoard::resetLayout;
	_resetFuncMap["BGridLayout"] = &PropertyBoard::resetLayout;

	connect(this, Signal_AssetChanged, &PropertyBoard::slotAssetChanged);
}

void PropertyBoard::slotAssetChanged(BObject* object, const BValue& value)
{
	for (int i = 0; i < this->widgetCount(); i++)
	{
		BWidget* panel = this->widget(i);
		panel->setAsset(0);
	}
	this->clearWidgets();
	if (BLayout* layout = value)
	{
		const char* className = layout->className();
		resetFunc func = _resetFuncMap[className];
		if (func)
		{
			(this->*func)(value);
		}
		return;
	}
	if (BWidget* widget = value)
	{
		const char* className = widget->className();
		resetFunc func = _resetFuncMap[className];
		if (func)
		{
			if (!_widgetPanel)
				_widgetPanel = new WidgetPanel();
			_widgetPanel->setAsset(value);
			this->addWidget(_widgetPanel);
			(this->*func)(value);
		}
		return;
	}
}

void PropertyBoard::resetDialog(BObject* object)
{
	if (!_dialogPanel)
		_dialogPanel = new DialogPanel();
	_dialogPanel->setAsset(object);
	this->addWidget(_dialogPanel);
}
void PropertyBoard::resetLabel(BObject* object)
{
	if (!_labelPanel)
		_labelPanel = new LabelPanel();
	_labelPanel->setAsset(object);
	this->addWidget(_labelPanel);
}
void PropertyBoard::resetButton(BObject* object)
{
	if (!_buttonPanel)
		_buttonPanel = new ButtonPanel();
	_buttonPanel->setAsset(object);
	this->addWidget(_buttonPanel);
}
void PropertyBoard::resetSpinBox(BObject* object)
{
	if (!_spinBoxPanel)
		_spinBoxPanel = new SpinBoxPanel();
	_spinBoxPanel->setAsset(object);
	this->addWidget(_spinBoxPanel);
}
void PropertyBoard::resetLayout(BObject* object)
{
	if (!_layoutPanel)
		_layoutPanel = new LayoutPanel();
	_layoutPanel->setAsset(object);
	this->addWidget(_layoutPanel);
}
